package com.cskaoyan.service.mall;


import com.cskaoyan.bean.BaseParam;

import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.mall.issue.Issue;
import com.cskaoyan.bean.mall.issue.IssueExample;
import com.cskaoyan.mapper.mall.IssueMapper;
import com.cskaoyan.utils.Constant;
import com.cskaoyan.utils.MarketPageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: enhui
 **/
@Service
public class IssueServiceImpl implements IssueService {
    @Autowired
    IssueMapper issueMapper;

    @Override
    public PageDataVo issueList(String question, BaseParam param) {
        MarketPageUtil.startPage(param);
        IssueExample issueExample = new IssueExample();
        IssueExample.Criteria criteria = issueExample.createCriteria();
        //条件查询，判空
        if (!StringUtils.isEmpty(question)) {
            criteria.andQuestionLike("%" + question + "%");
        }
        criteria.andDeletedEqualTo(false);
        //排序
        issueExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        return PageDataVo.ok(issues);
    }

    @Override
    public Issue createIssue(Issue issue) {
        Date date = new Date();
        issue.setAddTime(date);
        issue.setUpdateTime(date);
        int count = issueMapper.insertSelective(issue);
        if (count == 1) {
            return issue;
        }
        return null;
    }

    @Override
    public Issue updateIssue(Issue issue) {
        Date date = new Date();
        issue.setUpdateTime(date);
        //更新
        int count = issueMapper.updateByPrimaryKeySelective(issue);
        //查询并返回
        if (count == 1) {
            return issueMapper.selectByPrimaryKey(issue.getId());
        }
        return null;
    }

    /**
     * 逻辑删除，将delete字段的值从0改为1
     *
     * @param issue
     * @return
     */
    @Override
    public int deleteIssue(Issue issue) {
        issue.setDeleted(true);
        //记录的updateTime是当前时间
        Date date = new Date();
        issue.setUpdateTime(date);
        int count = issueMapper.updateByPrimaryKeySelective(issue);
        if (count == 1) {
            return Constant.SUCCESS;
        }
        return Constant.FAIL;
    }
}
